﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SpaceKinect
{
    class UFO:Ship
    {
        //rough estimate of the model's radius
        float radius = 0.75f;

        //used to calculate when to randomly change direction of UFO
        float lastChange = -1;

        //variables to hold the random change in each axis
        float xChange = 0;
        float yChange = 0;
        float zChange = 0;

        //how fast the UFO moves around
        int moveSpeed = 12;

        //Used to toggle firing
        public float FiringValue = 0;

        Random rnd = new Random();

        public UFO(Model m, Vector3 p, Quaternion q)
        {
            model = m;
            position = p;
            rotation = q;
            health = 100;
        }

        public void Update(float speed, GameTime gameTime, int mapHeight, int mapWidth, float playerPosition)
        {
            //vector that will randomly change the UFOs position
            Vector3 addVector;

            //generate random axis directions every second
            if ((gameTime.TotalGameTime.Seconds - lastChange) >= 1)
            {
                xChange = ((float)rnd.NextDouble() * 2 - 1);
                yChange = ((float)rnd.NextDouble() * 2 - 1) / moveSpeed;
                zChange = ((float)rnd.NextDouble() * 2 - 1) / moveSpeed;
                lastChange = gameTime.TotalGameTime.Seconds;
            }
            
            //keeps the UFO inside the tunnel and a certain distance range in front of player
            if (position.X - playerPosition < 35)
                xChange = Math.Abs(xChange);
            if (position.X - playerPosition > 65)
                xChange = -Math.Abs(xChange);
            if (position.Y < 1)
                yChange = -Math.Abs(yChange);
            if (position.Y > mapHeight - 1)
                yChange = Math.Abs(yChange);
            if (position.Z < -mapWidth + 1)
                zChange = Math.Abs(zChange);
            if (position.Z > -1)
                zChange = -Math.Abs(zChange);

            //Move the UFO forward at a similar rate to the player
            addVector = Vector3.Transform(new Vector3(1 + xChange, 0, 0), rotation);
            position += addVector * speed;

            //Move the UFO left/right and up/down (and yes, zChange and yChange are deliberately the wrong way around... was the only way to get it to work with our unorthodox axis system)
            addVector = Vector3.Transform(new Vector3(0, zChange, yChange), rotation);
            position += addVector;

            //Create boundingsphere around the UFO
            bound = new BoundingSphere(position, radius);
        }

        public void fireBullets()
        {

        }

        public override void Draw(Matrix projection, Matrix view)
        {
            Matrix world = Matrix.CreateScale(0.5f, 0.5f, 0.5f) * Matrix.CreateTranslation(-.045f, 0, 0) * Matrix.CreateRotationY(MathHelper.Pi) * Matrix.CreateRotationZ(MathHelper.Pi) * Matrix.CreateFromQuaternion(rotation) * Matrix.CreateTranslation(position);

            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.PreferPerPixelLighting = true;

                    effect.World = world;
                    effect.Projection = projection;
                    effect.View = view;
                }
                mesh.Draw();
            }
        }
    }
}
